#include <iostream>
#include <memory>
using namespace std;

class B;
class A;

class A
{
public:
    shared_ptr<B> pb;
    void fun()
    {
        cout << "A::fun()" << endl;
    }
    ~A()
    {
        cout << "A::~A()" << endl;
    }
};

class B
{
public:
    weak_ptr<A> pa; //使用weak_ptr
    void fun()
    {
        cout << "B::fun()" << endl;
    }
    ~B()
    {
        cout << "B::~B()" << endl;
    }
};

int main()
{
    shared_ptr<A> pa = make_shared<A>();
    shared_ptr<B> pb = make_shared<B>();
    pa->pb = pb;
    pb->pa = pa;
    pb->fun();
    pa->fun();
    return 0;
}

